package com.niannian.activity;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import com.niannian.BaseActivity;
import com.niannian.R;
import com.niannian.util.Common;
import com.niannian.util.UmengPage;
import com.niannian.view.CircleImageView;
import com.umeng.analytics.MobclickAgent;

/**
 * 我的
 * 
 * @author shenglin
 * 
 */
public class MyActivity extends BaseActivity {
	TextView tv_my_name;
	LinearLayout ll_my_info, ll_familym, ll_regards, ll_setting, ll_my_news;
	CircleImageView riv_my_avatar;
	public static boolean isForeground = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my);
		initTopView();
		initView();
		setLister();
	}

	// 头部视图
	private ImageView iv_top_left, iv_top_right;
	private TextView top_title;
	private LinearLayout iv_top_left2, top_title2;

	private void initTopView() {
		iv_top_left = (ImageView) findViewById(R.id.iv_top_left);
		iv_top_left2 = (LinearLayout) findViewById(R.id.iv_top_left2);
		iv_top_left.setVisibility(View.GONE);
		iv_top_left2.setVisibility(View.VISIBLE);

		top_title = (TextView) findViewById(R.id.top_title);
		top_title2 = (LinearLayout) findViewById(R.id.top_title2);
		top_title.setVisibility(View.GONE);
		top_title2.setVisibility(View.GONE);

		iv_top_right = (ImageView) findViewById(R.id.iv_top_right);
		iv_top_right.setVisibility(View.VISIBLE);
		iv_top_right.setImageResource(R.drawable.add_member);
		iv_top_right.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				initPopuwindow();
			}
		});
	}

	private void initView() {
		ll_my_info = (LinearLayout) findViewById(R.id.ll_my_info);
		riv_my_avatar = (CircleImageView) findViewById(R.id.riv_my_avatar);
		tv_my_name = (TextView) findViewById(R.id.tv_my_name);

		myApplication.finalbitmap.display(riv_my_avatar, Common.getAvatarThumbs(userInfoManager.avatar));
		tv_my_name.setText(userInfoManager.nickname + "");

		ll_familym = (LinearLayout) findViewById(R.id.ll_familym);
		ll_regards = (LinearLayout) findViewById(R.id.ll_regards);
		ll_setting = (LinearLayout) findViewById(R.id.ll_setting);

		ll_my_news = (LinearLayout) findViewById(R.id.ll_my_news);

	}

	private void setLister() {

		ll_my_info.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				openActivity(MyInfoEditActivity.class);
				getParent().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			}
		});
		ll_familym.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				openActivity(FamilyMemberActivity.class);
				getParent().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			}
		});

		ll_regards.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				openActivity(RegardsSendActivity.class);
				getParent().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			}
		});

		ll_setting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				openActivity(SettingActivity.class);
				getParent().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			}
		});
		ll_my_news.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				openActivity(MyFamilyNewsActivity.class);
				getParent().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			}
		});
	}

	PopupWindow RightPopupWindow;
	LinearLayout ll_family, ll_push_regards, ll_push_family_news, ll_add_date_tip;

	@SuppressWarnings("deprecation")
	private void initPopuwindow() {
		View mMenuView = null;
		if (RightPopupWindow == null) {
			mMenuView = LayoutInflater.from(MyActivity.this).inflate(R.layout.main_choose_popupwindow, null);

			ll_family = (LinearLayout) mMenuView.findViewById(R.id.ll_family);
			ll_push_regards = (LinearLayout) mMenuView.findViewById(R.id.ll_push_regards);
			ll_push_family_news = (LinearLayout) mMenuView.findViewById(R.id.ll_push_family_news);
			ll_add_date_tip = (LinearLayout) mMenuView.findViewById(R.id.ll_add_date_tip);

			ll_family.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Bundle bundle=new Bundle();
					bundle.putInt("ACTION_TYPE", 3);
					openActivity(AddFamilyMemberActivity.class,bundle);
					getParent().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
					RightPopupWindow.dismiss();
				}
			});
			ll_push_regards.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Bundle pBundle = new Bundle();
					pBundle.putInt("from", PushRegardsActivity.FROM_PUSH_REGARDS);
					openActivity(PushRegardsActivity.class, pBundle);
					getParent().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
					RightPopupWindow.dismiss();
					
					MobclickAgent.onEvent(MyActivity.this, UmengPage.plus_add_regard);
				}
			});
			ll_push_family_news.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Bundle pBundle = new Bundle();
					pBundle.putInt("from", PushRegardsActivity.FROM_push_news);
					openActivity(PushRegardsActivity.class, pBundle);
					getParent().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
					RightPopupWindow.dismiss();
				}
			});
			ll_add_date_tip.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					openActivity(AddDateTipActivity.class, DateTipActivity.PUSH_REGARDS_TIP);
					getParent().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
					RightPopupWindow.dismiss();
				}
			});
			RightPopupWindow = new PopupWindow(mMenuView, mScreenWidth / 2 + 60, LayoutParams.WRAP_CONTENT, true);
			RightPopupWindow.setBackgroundDrawable(new BitmapDrawable());
			RightPopupWindow.setOutsideTouchable(true);
			RightPopupWindow.setAnimationStyle(R.style.Popup_Animation_DownScale);
		} else {
			mMenuView = RightPopupWindow.getContentView();
		}

		RightPopupWindow.showAsDropDown(iv_top_right, -0, -0);

	}

	@Override
	public void onBackPressed() {
		exit();
	}

	@Override
	public void onResume() {
		super.onResume();
		isForeground = true;
		myApplication.finalbitmap.display(riv_my_avatar, Common.getAvatarThumbs(userInfoManager.avatar));
		tv_my_name.setText(userInfoManager.nickname + "");
		
		MobclickAgent.onPageStart(UmengPage.my_face);
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		isForeground = false;
		MobclickAgent.onPageEnd(UmengPage.my_face);
		MobclickAgent.onPause(this);
	}
}
